using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using CSI.Directory.Core.Model;

namespace CSI.Directory.Web.Controllers
{
    public class HomeController : BaseController
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            ViewData["Menu"] = new Menu() {Name = "Index"};
            ViewData["Article"] = new Article();
            ViewData["SubArticles"] = new List<Article>();
            var a = DirectoryService.GetArticlesForMenuByTitle("Index").OrderByDescending(t => t.CreateDate).ToList();
            if (a.Count > 0)
                ViewData["Article"] = a[0];
            return View();
        }

        public ActionResult B2B(string message)
        {
            return View();
        }

        public ActionResult Content(string id)
        {
            var menus = DirectoryService.GetAllMenus();
            var menu = menus.Where(t => t.Name.ToLower() == id.ToLower()).ToList();

            if (menu.Count == 0)
                return Index();

            ViewData["Menu"] = menu[0];
            ViewData["Article"] = new Article();
            ViewData["SubArticles"] = new List<Article>();
            var a = DirectoryService.GetArticlesForMenuByTitle(id).OrderByDescending(t=>t.CreateDate).ToList();
            //Are there articles?
            if (a.Count > 0)
            {
                var article = a.Where(t => t.Featured == true).ToList();
                //is there a featured?
                if (article.Count > 0)
                {
                    ViewData["Article"] = article[0];
                    ViewData["SubArticles"] = a.Where(t => t.Id != article[0].Id).ToList();
                }
                else
                {
                    ViewData["Article"] = a[0];
                    ViewData["SubArticles"] = a.Where(t => t.Id != a[0].Id).ToList();
                }
                
            }

            return View();
        }

        public ActionResult Article(int id, string name)
        {
            var article = DirectoryService.GetArticlesById(id);
            if (article == null)
                return Index();
            var menu = DirectoryService.GetMenusById(article.MenuId);

            if (menu == null)
                return Index();

            ViewData["Menu"] = menu;
            ViewData["Article"] = article;
            ViewData["SubArticles"] = new List<Article>();
            var a = DirectoryService.GetArticlesForMenuByTitle(menu.Name).OrderBy(t => t.CreateDate).ToList();
            //Are there articles?
            if (a.Count > 0)
            {
                ViewData["SubArticles"] = a.Where(t => t.Id != article.Id).ToList();

            }

            return View("Content");
        }

        public ActionResult Jobs()
        {
            return View("");
        }


        public ActionResult Feedback()
        {
            return View("");
        }
       
    }
}